package hjt.com.module_test.widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.os.Build;
import androidx.annotation.RequiresApi;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.tong.lib.utils.SizeUtils;

/**
 * Created by hjt on 2019/7/24.
 */

public class CustomScrollView extends RelativeLayout {

    private boolean isPlaying = true;
    private boolean isOk;
    private int lastY;
    private int delayY;
    /**
     * 滑动的距离
     */
    private int MOVE_DISTANCE = SizeUtils.dp2px(160);
    private View tvTop;
    private ViewGroup llCenter;
    private ViewGroup.MarginLayoutParams tvTopLayoutParams;
    private ViewGroup.MarginLayoutParams llCenterLayoutParams;
    private boolean isInit = true;
    private boolean isCanMove = true;


    public CustomScrollView(Context context) {
        super(context);
    }

    public CustomScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public CustomScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onFinishInflate() {
        initChildren();
        super.onFinishInflate();
    }

    public void initChildren() {
        if (getChildCount() >= 2) {
            isOk = true;
            tvTop = getChildAt(0);
            llCenter = (ViewGroup) getChildAt(1);
            tvTopLayoutParams = (MarginLayoutParams) tvTop.getLayoutParams();
            llCenterLayoutParams = (MarginLayoutParams) llCenter.getLayoutParams();
        }

    }

    public void setPlaying(boolean playing) {
        isPlaying = playing;
    }

    public void setCanMove(boolean canMove) {
        isCanMove = canMove;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (isOk && !isPlaying && isCanMove) {
            int nowY = (int) ev.getY();
            switch (ev.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    lastY = nowY;
                    break;
                case MotionEvent.ACTION_MOVE:
                    if (lastY == 0) {
                        lastY = nowY;
                    }
                    delayY = nowY - lastY;
                    if (delayY == 0) break;

                    if (isInit) {
                        if (delayY > 0) {
                            delayY = 0;
                        }
                        if (-delayY > MOVE_DISTANCE) {
                            delayY = -MOVE_DISTANCE;
                        }
                        llCenterLayoutParams.topMargin = delayY;
                    } else {
                        if (delayY < 0) {
                            delayY = 0;
                        }
                        if (delayY > MOVE_DISTANCE) {
                            delayY = MOVE_DISTANCE;
                        }
                        llCenterLayoutParams.topMargin = delayY - MOVE_DISTANCE;
                    }
                    llCenter.setLayoutParams(llCenterLayoutParams);

                    break;
                case MotionEvent.ACTION_UP:
                    lastY = 0;
                    if (isInit) {
                        if (Math.abs(delayY) >= MOVE_DISTANCE / 2) {
                            isInit = false;
                        }
                    } else {
                        if (Math.abs(delayY) >= MOVE_DISTANCE / 2) {
                            isInit = true;
                        }
                    }

                    if (isInit) {
                        ValueAnimator valueAnimator = ValueAnimator.ofInt(llCenterLayoutParams.topMargin, 0);
                        valueAnimator.setDuration(300);
                        valueAnimator.addUpdateListener(animation -> {
                            int value = (int) animation.getAnimatedValue();
                            int offset = value - llCenterLayoutParams.topMargin;
                            Log.e("onTouch_1", offset + "");
                            llCenterLayoutParams.topMargin = value;
                            llCenter.setLayoutParams(llCenterLayoutParams);
//                            if (value == 0) {
//                                mTop.setVisibility(GONE);
//                            }
                        });
                        valueAnimator.start();
                    } else {
                        ValueAnimator valueAnimator = ValueAnimator.ofInt(llCenterLayoutParams.topMargin, -MOVE_DISTANCE);
                        valueAnimator.setDuration(300);
                        valueAnimator.addUpdateListener(animation -> {
                            int value = (int) animation.getAnimatedValue();
                            int offset = value - llCenterLayoutParams.topMargin;
                            Log.e("onTouch_2", offset + "");
                            llCenterLayoutParams.topMargin = value;
                            llCenter.setLayoutParams(llCenterLayoutParams);
                            if (value == -MOVE_DISTANCE) {
//                                mTop.setAlpha(1);
                                isCanMove = false;
                            }
                        });
                        valueAnimator.start();
                    }

                    break;
            }
        }
        return super.dispatchTouchEvent(ev);
    }
}
